package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cssl.dto.NewUserStatisticsDTO;
import com.cssl.dto.UserCreateDTO;
import com.cssl.dto.UserRechargeDTO;
import com.cssl.entity.User;
import com.cssl.exception.BusinessException;
import com.cssl.service.UserService;
import com.cssl.util.UtilEncryption;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RefreshScope
@RestController
@Tag(name = "用户控制器", description = "用户相关的 API")
public class UserController {
    @Autowired
    private UserService us;

    /**
     * 用户登陆
     * @param phone
     * @param cipher
     * @return
     */
    @PostMapping ("/login")
    public User login(@RequestParam("phone") String phone,
                      @RequestParam("cipher") String cipher){
        //加密
        String cip = "";
        try {
            cip = UtilEncryption.encrypt(cipher);
        } catch (Exception e) {
            System.out.println("加密失败");
            throw new RuntimeException(e);
        }

        User user = us.getOne(new QueryWrapper<User>()
                .eq("phone",phone)
                .eq("cipher",cip));
        return user;
    }

    /**
     * 查询所有用户
     * @return
     */
    @PostMapping("/findUser")
    public List<User> findUser(){
        return us.list();
    }

    /**
     * 创建用户
     * @param userDto
     * @return
     */
    @PostMapping("/createUser")
    @Operation(summary = "创建用户")
    public String createUser(UserCreateDTO userDto){
        try {
            us.createUser(userDto);
            return "添加成功";
        } catch (BusinessException e) {
            return "用户已存在";
        }
    }

    /**
     * 余额操作 （带乐观锁） 充值
     * @return
     */
    @PostMapping("/recharge")
    @Operation(summary = "充值")
    public String recharge(UserRechargeDTO urDto){
        try {
            BigDecimal newBalance = us.recharge(urDto);
            return newBalance.toString();
        } catch (Exception e) {
            return "余额操作失败";
        }
    }

    /**
     * 余额操作 （带乐观锁） 扣减余额
     * @return
     */
    @PostMapping("/deductionMoney")
    public String deductionMoney(@RequestParam("userId") Integer userId,
                           @RequestParam("amount") BigDecimal amount){
        try {
            BigDecimal newBalance = us.deductionMoney(userId,amount);
            return newBalance.toString();
        } catch (Exception e) {
            return "余额操作失败";
        }
    }

    /**
     * 余额操作 （带乐观锁） 扣减余额
     * @return
     */
    @PostMapping("/deductionM")
    public String deductionM(@RequestParam("userId") Integer userId,
                             @RequestParam("amount") BigDecimal amount,
                             @RequestParam("po_id") String po_id){
        try {
            BigDecimal newBalance = us.deductionM(userId,amount,po_id);
            return newBalance.toString();
        } catch (Exception e) {
            return "余额操作失败";
        }
    }

    /**
     * 获取新用户注册数
     * @return
     */
    @PostMapping("/newUserStatistics")
    public NewUserStatisticsDTO newUserStatistics(){
        return new NewUserStatisticsDTO(
                us.findNewUser(),
                us.findNewUser_yd()
        );
    }
}
